package com.projeto.business.controller;

import javax.ejb.Stateless;
import javax.persistence.Query;

import com.projeto.infra.business.GenericBean;
import com.projeto.persistence.Customer;
import com.projeto.persistence.Customer.CustomerType;
import com.projeto.persistence.User;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;

@Stateless
public class CustomerController extends GenericBean<Customer> {



    public CustomerController(){
        super();
    }
    
    /* (non-Javadoc)
     * @see com.novo.projeto.business.IUserBean#find(java.lang.String)
     */
    public Customer find(String id) {
        return getEntityManager().find(Customer.class, id);
    }

    /* (non-Javadoc)
     * @see com.novo.projeto.business.IUserBean#findAll()
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<Customer> findAll() {
        Query query = em.createQuery("SELECT e FROM Customer e");
        return (List<Customer>) query.getResultList();
    }

    @SuppressWarnings("unchecked")
    public List<Customer> findAllActive() {
        Query query = em.createQuery("SELECT e FROM Customer e WHERE e.inactive = 0");
        return (List<Customer>) query.getResultList();
    }
    
    public List<Customer> findActiveByNameAndType(String name, CustomerType type) {
        Query query = em.createQuery("SELECT e FROM Customer e WHERE e.inactive = 0 AND e.type =:type AND e.firstName LIKE '" + name + "%'");
        query.setParameter("type", type);
        return (List<Customer>) query.getResultList();
    }
    
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    @Override
    public void save(Customer customer) {

        String userLogin = ctx.getCallerPrincipal().getName();
        User user = em.find(User.class, userLogin);
        Date updatedDt = new Date();
        if (customer == null) {
            throw new IllegalArgumentException("Customer cannot be null");
        }

        Customer temp = find(customer.getId() != null ? customer.getId() : "");
        if (temp == null) {
            temp = customer;
            temp.setId(UUID.randomUUID().toString());
            temp.setCreateDt(updatedDt);
            temp.setCreatedBy(user);
            //@TODO add type as Enumeration
            //temp.setType(Customer.CustomerType.SHIPPER);
        } else {
            temp.setInactive(customer.getInactive());
            temp.setAirRate(customer.getAirRate());
            temp.setAlias(customer.getAlias());
            temp.setCelular(customer.getCelular());
            temp.setCity(customer.getCity());
            temp.setCnpjcpf(customer.getCnpjcpf());
            temp.setComments(customer.getComments());
            temp.setCompany(customer.getCompany());
            temp.setCountry(customer.getCountry());
            temp.setCubic(customer.getCubic());
            temp.setEmail(customer.getEmail());
            temp.setFax(customer.getFax());
            temp.setFirstName(customer.getFirstName());
            temp.setIerg(customer.getIerg());
            temp.setLastName(customer.getLastName());
            temp.setMiddleInitial(customer.getMiddleInitial());
            temp.setOceanRate(customer.getOceanRate());
            temp.setOtherPhone(customer.getOtherPhone());
            temp.setPhone(customer.getPhone());
            temp.setPickupRate(customer.getPickupRate());
            temp.setState(customer.getState());
            temp.setStreet(customer.getStreet());
            temp.setSuite(customer.getSuite());
            temp.setType(customer.getType());
                        //@TODO add type as Enumeration
            //temp.setType(Customer.CustomerType.SHIPPER);
            temp.setZip(customer.getZip());
        }

        temp.setUpdatedBy(user);
        temp.setUpdateDt(updatedDt);

        // @TODO insert logic to  add Events
        em.persist(temp);
        System.out.println(temp.toString());
    }

    @Override
    protected boolean validate(Customer e) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}
